﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;

using RTW.Core.Extensions;
using RTW.Models;
using RTW.Servers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace RTW.Web.Component
{
    public class TopRoleMenuList : ViewComponent
    {
        TMenuServer _tmenuServer;
        TRoleServer _roleServer;
        private IMemoryCache _memoryCache;
        private string cacheKey = "TopRoleMenuList";


        public TopRoleMenuList(IMemoryCache memoryCache)
        {
            _tmenuServer = new TMenuServer();
            _roleServer = new TRoleServer();
            _memoryCache = memoryCache;
        }

        public IViewComponentResult Invoke(string userid)
        {
            var items = new List<TMenu>();
            if (!_memoryCache.TryGetValue(cacheKey, out items)&& items?.Count == 0)
            {
                items = _tmenuServer.GetMenuTreeByUser(userid);
            }
            _memoryCache.Set(cacheKey, items, TimeSpan.FromMinutes(10));
            return View(items);
        }


    }




}
